package com.uenpay.im.ui.old.activity.mycollect;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.aswife.ui.RoundedImageView;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.weight.edittext.Emoji;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;

import java.util.ArrayList;

public class MyColletListAdapter extends BaseAdapter {

	private final int COLLECT_TEXT = 0;
	private final int COLLECT_IMAGE = 1;
	private final int COLLECT_MOVEMENT = 2;

	private LayoutInflater inflater;
	private ArrayList<MyCollect> arrayList;

	private Context context;
	private Emoji mEmoji;

	public MyColletListAdapter(Context context, ArrayList<MyCollect> collects) {
		this.inflater = LayoutInflater.from(context);
		this.arrayList = collects;
		this.context = context;
		mEmoji = new Emoji();
	}

	public void setData(ArrayList<MyCollect> collects) {
		this.arrayList = collects;
		notifyDataSetChanged();
	}

	public void addData(ArrayList<MyCollect> collects) {
		this.arrayList.addAll(collects);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList.size();
	}

	@Override
	public MyCollect getItem(int position) {
		return arrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		MyCollect myCollect = arrayList.get(position);
		int type = myCollect.moduleid;
		if (type == 103) {
			if (TextUtils.isEmpty(myCollect.imgurl)) {
				type = 1;
			} else {
				type = 2;
			}
		}
		switch (type) {
		case 113:
			return COLLECT_MOVEMENT;
		case 1:
			return COLLECT_TEXT;
		case 2:
			return COLLECT_IMAGE;
		default:
			return COLLECT_TEXT;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BaseCollectViewHodler baseCollectViewHodler = new BaseCollectViewHodler();
		ImageCollectViewHodler imageCollectViewHodler = null;
		TextCollectViewHodler textCollectViewHodler = null;
		MovementCollectViewHolder movementCollectViewHolder = null;
		MyCollect myCollect = arrayList.get(position);
		int type = getItemViewType(position);
		if (convertView == null) {
			switch (type) {
			case COLLECT_TEXT:
				textCollectViewHodler = new TextCollectViewHodler();
				convertView = inflater.inflate(R.layout.collect_list_item_text,
						null);
				textCollectViewHodler.collect_textview = (TextView) convertView
						.findViewById(R.id.collet_content);
				textCollectViewHodler.from_cust_name = (TextView) convertView
						.findViewById(R.id.from_cust_name);
				textCollectViewHodler.collect_time = (TextView) convertView
						.findViewById(R.id.collect_time);
				textCollectViewHodler.from_cust_avatar = (RoundedImageView) convertView
						.findViewById(R.id.from_cust_avatar);
				convertView.setTag(textCollectViewHodler);
				break;
			case COLLECT_IMAGE:
				imageCollectViewHodler = new ImageCollectViewHodler();
				convertView = inflater.inflate(
						R.layout.collect_list_item_image, null);
				imageCollectViewHodler.collect_imageview = (MaskImageView) convertView
						.findViewById(R.id.collect_iv);
				imageCollectViewHodler.from_cust_name = (TextView) convertView
						.findViewById(R.id.from_cust_name);
				imageCollectViewHodler.collect_time = (TextView) convertView
						.findViewById(R.id.collect_time);
				imageCollectViewHodler.from_cust_avatar = (RoundedImageView) convertView
						.findViewById(R.id.from_cust_avatar);
				convertView.setTag(imageCollectViewHodler);
				break;
			case COLLECT_MOVEMENT:
				movementCollectViewHolder = new MovementCollectViewHolder();
				convertView = inflater.inflate(
						R.layout.collect_list_item_movement, null);
				movementCollectViewHolder.movementIv = (MaskImageView) convertView
						.findViewById(R.id.movement_iv);
				movementCollectViewHolder.from_cust_name = (TextView) convertView
						.findViewById(R.id.from_cust_name);
				movementCollectViewHolder.movementTv = (TextView) convertView
						.findViewById(R.id.movement_title);
				movementCollectViewHolder.collect_time = (TextView) convertView
						.findViewById(R.id.collect_time);
				movementCollectViewHolder.from_cust_avatar = (RoundedImageView) convertView
						.findViewById(R.id.from_cust_avatar);
				convertView.setTag(movementCollectViewHolder);
				break;
			}
		} else {
			switch (type) {
			case COLLECT_TEXT:
				textCollectViewHodler = (TextCollectViewHodler) convertView
						.getTag();
				break;
			case COLLECT_IMAGE:
				imageCollectViewHodler = (ImageCollectViewHodler) convertView
						.getTag();
				break;
			case COLLECT_MOVEMENT:
				movementCollectViewHolder = (MovementCollectViewHolder) convertView
						.getTag();
				break;
			}
		}
		baseCollectViewHodler = (BaseCollectViewHodler) convertView.getTag();
		if (baseCollectViewHodler != null) {
			baseCollectViewHodler.from_cust_avatar
					.SetUrl(APIConfiguration.getAvatarUrlNormal(
							myCollect.fcustid, PacketType.PERSONAL));
			baseCollectViewHodler.from_cust_name.setText(myCollect.author);
			baseCollectViewHodler.collect_time.setText(myCollect.createtime);
		}

		switch (type) {
		case COLLECT_TEXT:
			textCollectViewHodler.collect_textview.setText(mEmoji
					.getSpannedRate(myCollect.content, context, 2));
			break;
		case COLLECT_IMAGE:
			imageCollectViewHodler.collect_imageview.SetUrl(myCollect.imgurl);
			break;
		case COLLECT_MOVEMENT:
			movementCollectViewHolder.movementIv.SetUrl(myCollect.imgurl);
			movementCollectViewHolder.movementTv.setText(myCollect.title);
			break;
		}

		return convertView;
	}

	private class BaseCollectViewHodler {
		RoundedImageView from_cust_avatar;
		TextView from_cust_name;
		TextView collect_time;
	}

	private class ImageCollectViewHodler extends BaseCollectViewHodler {
		private MaskImageView collect_imageview;
	}

	private class TextCollectViewHodler extends BaseCollectViewHodler {
		private TextView collect_textview;
	}

	private class MovementCollectViewHolder extends BaseCollectViewHodler {
		private MaskImageView movementIv;
		private TextView movementTv;
	}

}
